/*
 * Sonatype Nexus (TM) Open Source Version
 * Copyright (c) 2008-present Sonatype, Inc.
 * All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
 *
 * This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
 * which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
 *
 * Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
 * of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
 * Eclipse Foundation. All other trademarks are the property of their respective owners.
 */
package org.sonatype.nexus.repository.vulnerability;

import java.io.Serializable;
import java.util.Objects;

/**
 * Component model to analyze on vulnerability.
 *
 * @since 3.24
 */
public class AuditComponent
    implements Serializable
{
  private static final long serialVersionUID = 470774542768031901L;

  private final String packageType;

  private final String name;

  private final String version;

  private final String appId;

  /**
   * Constructor with parameters.
   *
   * @param appId       the IQ Server application id.
   * @param packageType can be "npm", "maven", etc.
   * @param name        the name of component.
   * @param version     the version of the component.
   */
  public AuditComponent(final String appId, final String packageType, final String name, final String version) {
    this.packageType = packageType;
    this.name = name;
    this.version = version;
    this.appId = appId;
  }

  public String getAppId() {
    return appId;
  }

  public String getPackageType() {
    return packageType;
  }

  public String getName() {
    return name;
  }

  public String getVersion() {
    return version;
  }

  public String getPackageUrl() {
    return "pkg:" + packageType + "/" + name + "@" + version;
  }

  @Override
  public boolean equals(final Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    AuditComponent auditComponent = (AuditComponent) o;
    return Objects.equals(appId, auditComponent.appId) &&
        Objects.equals(packageType, auditComponent.packageType) &&
        Objects.equals(name, auditComponent.name) &&
        Objects.equals(version, auditComponent.version);
  }

  @Override
  public int hashCode() {
    return Objects.hash(appId, packageType, name, version);
  }

  @Override
  public String toString() {
    return String.format("appId [%s] type [%s] name [%s] version [%s]", appId, packageType, name, version);
  }
}
